package com.miku.screens;

import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.miku.R;
import com.miku.logic.ApplicationGlobal;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class LoginActivity extends Activity {
	String appId= "193535254031744";
	String URL = "http://www.devosinc.com/miku/";
	
	private Runnable llamadaRestLogin;
	private ProgressDialog progressDialog = null;
	
	Facebook facebook = new Facebook(appId);
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		facebook.authorize(this, new String[] {"email","user_birthday","user_hometown","user_relationships"},
			new DialogListener(){

				public void onComplete(Bundle values) {
					Log.i("LoginActivity", "Entra a FB onComplete");
					llamadaRestLogin = new Runnable(){

						public void run() {
							((ApplicationGlobal)getApplication()).setFacebook(facebook);
							((ApplicationGlobal)getApplication()).setUrlRest(URL);
							try{
								loginRest();
								Log.i("LoginActivity", "salio de la llamada rest");
								runOnUiThread(retornoLogin);
							}catch(Exception e){
								Log.e("LoginActivity", "Excepcion: " + e.getMessage());
							}
						}
					};
					new Thread(llamadaRestLogin).start();
					progressDialog = ProgressDialog.show(LoginActivity.this, 
							LoginActivity.this.getText(R.string.progress_login_titulo), 
							LoginActivity.this.getText(R.string.progress_login_mensaje), true);
					
				}

				public void onFacebookError(FacebookError e) {
					Log.e("LoginActivity", "OnFacebookError : " + e.getMessage());
					Toast.makeText(LoginActivity.this, 
							LoginActivity.this.getString(R.string.error_login_facebook) , 
							Toast.LENGTH_LONG).show();
					
				}

				public void onError(DialogError e) {
					Log.e("LoginActivity", "OnError : " + e.getMessage());
					Toast.makeText(LoginActivity.this, 
							LoginActivity.this.getString(R.string.error_login) , 
							Toast.LENGTH_LONG).show();
					
				}

				public void onCancel() {
					
					
				}
				
			});
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		facebook.authorizeCallback(requestCode, resultCode, data);
	}
	
	private Runnable retornoLogin = new Runnable() {

		public void run() {
			progressDialog.dismiss();
			Intent intent = new Intent();
            intent.setClass(LoginActivity.this, DashboardActivity.class);
            startActivity(intent);
            Log.i("LoginActivity", "Debio iniciar la dashboard activity");
			
		}
	};
	
	private void loginRest() throws InterruptedException{
		//TODO Se debe de llamar a login Rest
		Thread.sleep(10000l);
	}
}
